import scrapy
from ImageSpider.items import ImageItem
from ImageSpider.settings import DEFAULT_REQUEST_HEADERS
import time
class MmzzttSpider(scrapy.Spider):
    name = 'msgao'
    allowed_domains = ['mmzztt.com']
    start_urls = ['https://mmzztt.com/photo/']

    def parse(self,response):
        # article>div.uk-card a.uk-inline
        links=response.css('//article/div[@class="uk-card"]/div[@class="uk-card-media-top"]/a[@class="uk-inline"]/@href').extract()
        for link in links:
            yield scrapy.Request(url=response.urljoin(link),callback=self.parse_item)
        # 下一页
        next_link=response.xpath('//ul[@class="uk-pagination uk-flex-center uk-margin-remove uk-visible@s"]/li/a')
        if next_link[-2].xpath('span/text()').extract_first()=='下一页':
            yield scrapy.Request(url=response.urljoin(next_link[-1].xpath('@href').extract_first()), callback=self.parse,dont_filter=True)

    def parse_item(self, response):
        # 下一页
        # 下一张图，为js操作

        item = ImageItem()
        item['title']=response.xpath('//h1[@class="uk-article-title uk-text-truncate"]/text()').extract_first()
        item['image_urls']=response.xpath('//figure[@class="uk-inline"]/img/@src').extract_first()
        item['imgAlt']=response.xpath('//figure[@class="uk-inline"]/img/@alt').extract_first()
        item['referer']=response.url
        if item['image_urls']==None:
            yield Request(url=response.url, callback=self.parse_item)
        else:
            yield item

# 01h 02d 03a 04i 05e